package com.example.springboot.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.springboot.common.Result;
import com.example.springboot.common.ResultCode;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.domain.po.Account;
import com.example.springboot.domain.po.Admin;
import com.example.springboot.domain.po.User;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.AccountService;
import com.example.springboot.service.AdminService;
import com.example.springboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.controller
 * @Project：springboot
 * @name：AccountController
 * @Date：2025/2/5 10:28
 * @Filename：AccountController
 * @for:
 */
@Api(tags = "管理员管理接口")
@RestController
@CrossOrigin
@RequestMapping("/account")
public class AccountController {
    @Resource
    private AccountService accountService;
    @Resource
    private UserService userService;
     @Resource
    private AdminService adminService;

    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result accountLogin(@RequestBody Account account) {
        Map<String, Object> map = accountService.JWTLogin(account);
        return Result.success(map); //装好数据后，把map结合返回给前端

    }

    /**
     * 注册接口
     *
     * @param account
     * @return
     */
    @ApiOperation("用户注册接口")
    @PostMapping("/register")
    public Result userRegister(@RequestBody Account account) {
        //转成User类
        User user = BeanUtil.copyProperties(account, User.class);
        userService.userRegister(user);
        return Result.success();
    }

    /**
     * 描述：获取当前登录用户信息
     */
    @GetMapping("/getUser")
    public Result getAccountInfo() {
        Account loginUser = JwtTokenUtils.getCurrentUser();
        if (loginUser == null) {
            return Result.success(new Object());
        }
        Integer role = loginUser.getRole();
        if (1 == role) {
            return Result.success(adminService.findById(loginUser.getId()));
        }
        if (2 == role) {
            return Result.success(userService.findById(loginUser.getId()));
        }

        return Result.success(new Object());
    }


    /**
     * 描述：更新密码接口
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account info) {
        //        1.获取当前登录用户 判断是否登录
        Account account = JwtTokenUtils.getCurrentUser();
        if (account == null) {
            return Result.error(ResultCode.USER_NOT_EXIST_ERROR.msg);
        }
        //        2。旧密码校验：获取前端传过来的要修改用户的旧密码
        String oldPassword = info.getPassword();
        if (!oldPassword.equals(account.getPassword())) {
            return Result.error(ResultCode.PARAM_PASSWORD_ERROR.msg);
        }
        //        3.重置密码：旧密码校验通过后，根据角色判断管理员还是用户，重置数据库用户的密码
        Integer role = account.getRole();
        if (1 == role) {
            Admin admin = new Admin();
            BeanUtils.copyProperties(account, admin);
            admin.setPassword(info.getNmm());
            adminService.update(admin);
        }
        if (2 == role) {
            User user = new User();
            BeanUtils.copyProperties(account, user);
            user.setPassword(info.getNmm());
            userService.update(user);
        }

        return Result.success();
    }


}
